package com.example.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    public static String md5(String text) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("md5");
            StringBuilder stringBuilder = new StringBuilder();
            byte[] ByText = digest.digest(text.getBytes());
            for (byte item:ByText) {
                int b = item & 0xFF;
                String Hex = Integer.toHexString(b);
                if (Hex.length()==1){
                    stringBuilder.append("0").append(Hex);
                }else {
                    stringBuilder.append(Hex);
                }
            }
            return stringBuilder.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }
}
